import platform = require('../../../../platform/common/baseCommon');
import bpCommon = require('../../../../platform/common/bpCommon');
import appClient = require('../../../../platform/client/appClient');
import loginView = require('../../views/userinfo/loginView');
import { error } from 'util';

let moduleName = "platform.client";
let loginFactory = new appClient.asyncFactory(bpCommon.IUserLogin, "remoteCall", "loginService");
let login = <bpCommon.IUserLogin>loginFactory.getValue();
/**
 * 测试视图
 */
@platform.addon("我的—未登录视图", "我的—未登录测试视图", moduleName)
@appClient.template('views/userInfo.html')
export class userInfoView extends appClient.baseControl {
    $btnLogin: JQuery = undefined;
    $cmpLogin: JQuery = undefined;
    $cmpUser: JQuery = undefined;

    $lblusername: JQuery = undefined;

    $btnNsrReport: JQuery = undefined;
    $btnLoan: JQuery = undefined;
    $btnSetting: JQuery = undefined;
    $btnLogout: JQuery = undefined;
    view: appClient.baseControl = new loginView.loginView();

    btnLogin_click?() {
        appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/loginViewConfig', '登录');
    }

    // function GetQueryString(name) {
    //     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //     var r = window.location.search.substr(1).match(reg);
    //     if (r != null)
    //         return decodeURI(r[2]);   //对参数进行decodeURI解码
    //     return null;
    // };

    onShowed?() {
        var swrydm;
        var reg = new RegExp("(^|&)" + "swrydm" + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            swrydm = decodeURI(r[2]);   //对参数进行decodeURI解码
        }
        if (swrydm) {
            alert("用户名为：" + swrydm);
        }


        if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
            this.$lblusername.text(platform.currentUser.userinfo.name);
        }

        if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
            this.$cmpLogin.hide();
            this.$cmpUser.show();
            this.$btnLogout.show();
        } else {
            this.$cmpLogin.show();
            this.$cmpUser.hide();
            this.$btnLogout.hide();
        }
    }


    onAfterBind?() {
        // if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
        //     this.$cmpLogin.hide();
        //     this.$cmpUser.show();
        // } else {
        //     this.$cmpLogin.show();
        //     this.$cmpUser.hide();
        // }

        this.$btnLogin.click(() => {

            this.btnLogin_click();
        });

        this.$btnNsrReport.click(() => {
            if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
                alert("纳税信用报告!");
            } else {
                appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/loginViewConfig', '登录');
            }
        });
        this.$btnLoan.click(() => {
            if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
                alert("贷款进度!");
            } else {
                appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/loginViewConfig', '登录');
            }
        });
        this.$btnSetting.click(() => {
            if (platform.currentUser && platform.currentUser.userinfo.status == "00") {
                alert("设置!");
            } else {
                appClient.mainApplication.mainForm.navigate('./project/test/config/client/mobileapp/userinfo/loginViewConfig', '登录');
            }
        });
        this.$btnLogout.click(() => {
            try {
                // let rq =new platform.RequestMessage<platform.userToken>();
                // rq.body=platform.currentUser;
                login.logout(platform.currentUser)
                    .then(rs => {
                        if (rs.header.rtnCode == "00") {
                            platform.currentUser = null;
                            this.onShowed();
                            console.log("登出成功！");
                        } else {
                            console.log("登出接口状态：" + rs.header.rtnCode + ";错误信息：" + rs.header.rtnMsg.message);
                            alert(rs.header.rtnMsg.message);
                        }
                    }
                    ).catch(error => {
                        console.log(error.message.responseText);
                        alert("登出失败：" + JSON.parse(error.message).responseText);
                    });
            } catch (error) {
                console.log(error.message.responseText);
                alert("登出失败：" + JSON.parse(error.message).responseText);
            }
        });
    }
}